<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<% 
	String context = request.getContextPath(); 
%>
<html>
<head>
  	<title>子任务节点</title>
	<link rel="stylesheet" type="text/css" href="<%=context %>/Source/lib/ligerUI/skins/ligerui-icons.css" />
	<link rel="stylesheet" type="text/css" href="<%=context %>/Source/lib/ligerUI/skins/Aqua/css/ligerui-all.css" />
</head>
<body>
	<table style="border-collapse:separate; border-spacing:10px;margin:10px auto;">
	  	<tr>
	    	<td>工作任务：</td>
	    	<td>
	        	<input type="text" id="workTask" class="liger-textbox" name="workTask" style="width: 170px;" />
	    	</td>
	  	</tr>
	  	<tr>
	    	<td>下个节点：</td>
	    	<td>
	      		<select id="node" style="width: 173px;" class="liger-textbox"></select>
	    	</td>
	  	</tr>
	  	<tr>
	    	<td>执行方法：</td>
	    	<td>
	      		<input type="hidden" id="functionId" name="functionId" />
	      		<input type="text" id="functionName" name="functionName" />
	    	</td>
	  	</tr>
	  	<tr>
	    	<td>编辑页面：</td>
	    	<td>
	      		<input type="hidden" id="editPageId" name="editPageId" />
	      	<input type="text" id="editPageName" name="editPageName" />
	    	</td>
	  	</tr>
	</table>
	<div id="navtab1" style="width: 99%; overflow: hidden;margin:0 auto; border: 1px solid #D3D3d3;" class="liger-tab">
		<div tabid="home" title="任务执行人" lselected="true" style="padding:10px;">
	    	<input type="radio" id="role" name="fkType" value="group" checked="checked" />角色&nbsp;&nbsp;
	    	<input type="radio" id="organizational" name="fkType" value="group" />组织机构&nbsp;&nbsp;
	    	<input type="radio" id="executive" name="fkType" value="user" />自定义执行人&nbsp;&nbsp;
	    	<input type="radio" id="promoter" name="fkType" value="user" />流程发起人
	    	<div>
	        	<%--角色--%>
		        <div class="post" style="margin-top:10px;">
		            <div>
		                <input type="button" value="添加" id="addRole" onclick="f_addRloe()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
		                <input type="button" value="删除" id="deleteRole" onclick="f_deleteRole()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
		
		            </div>
		            <div id="mainGridRole"style="margin-top:10px;">
		
		            </div>
		        </div>
		        <%--组织机构--%>
		        <div class="post" style="margin-top:10px;display:none;">
		            <div>
		                <input type="button" value="添加" id="addOrganization" onclick="f_addOrganization()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
		                <input type="button" value="删除" id="deleteOrganization" onclick="f_deleteOrganization()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
		            </div>
		            <div id="mainGridOrganization"style="margin-top:10px;">
		
		            </div>
		        </div>
		        <%--自定义执行人--%>
		        <div class="post" style="margin-top:10px;display:none;">
		            <div>
		                <input type="button" value="添加" id="addUser" onclick="f_addUser()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
		                <input type="button" value="删除" id="deleteUser" onclick="f_deleteUser()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
		            </div>
		            <div id="mainGridUser"style="margin-top:10px;">
		
		            </div>
		        </div>
		        <%--流程发起人--%>
		        <div class="post" style="margin-top:10px;display:none;text-align: center;"> 备注：由流程发起人执行任务</div>
	    	</div>
		</div>
		
		<div title="启动条件" style="padding:10px;">
	      	<input type="button" value="添加" id="addStartCondition" onclick="addStartCondition()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
	      	<input type="button" value="删除" id="deleteStartCondition" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
	      	<div>表达式：</div>
	      	<div id="startCondition"></div>
	  	</div>
	  	
		<div title="输出条件" style="padding:10px;">
			<input type="button" value="添加" id="addConditions" onclick="conditionsDialog()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
			<input type="button" value="删除" id="delete" onclick="f_deleteExpress()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
			<div id="mainGridConditions"style="margin-top:10px;"></div>
		</div>
	
	    <div title="通知" style="padding:10px;">
	        <input type="radio" id="noticeRole" name="notice" value="role" checked="checked"/>通知角色&nbsp;&nbsp;
	        <input type="radio" id="noticeExecutive" name="notice" value="user" />通知执行人&nbsp;&nbsp;
	        <input type="radio" id="noticePromoter" name="notice" value="user" />通知发起人
	        <div>
	            <%--通知角色--%>
	            <div class="notice" style="margin-top:10px;">
	                <table style="border-collapse:separate; border-spacing:10px;">
	                    <tr>
	                        <td>通知类型：</td>
	                        <td>
	                            <input type="text" id="noticeRoleWay" class="liger-textbox" name="noticeWay" style="width: 170px;background:#ffffff;" value="Email" disabled="disabled" />
	                        </td>
	                    </tr>
	                    <tr>
	                        <td>通知标题：</td>
	                        <td>
	                            <input type="text" id="noticeRoleTitle" class="liger-textbox" name="noticeTitle" style="width: 170px;" />
	                        </td>
	                    </tr>
	                    <tr>
	                        <td valign="top">通知内容：</td>
	                        <td>
	                            <textarea id="noticeRoleContent" name="noticeContent" ></textarea>
	                        </td>
	                    </tr>
	                </table>
	                <div>
	                    <input type="button" value="添加" id="addNoticeRole" onclick="f_addNoticeRloe()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
	                    <input type="button" value="删除" id="deleteNoticeRole" onclick="f_deleteNoticeRole()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
	                </div>
	                <div id="mainGridNoticeRole"style="margin-top:10px;">
	
	                </div>
	            </div>
	            <%--通知执行人--%>
	            <div class="notice" style="margin-top:10px;display:none;">
	                <table style="border-collapse:separate; border-spacing:10px;">
	                    <tr>
	                        <td>通知类型：</td>
	                        <td>
	                            <input type="text" id="noticeUserWay" class="liger-textbox" name="noticeWay" style="width: 170px;background:#ffffff;" value="Email" disabled="disabled" />
	                        </td>
	                    </tr>
	                    <tr>
	                        <td>通知标题：</td>
	                        <td>
	                            <input type="text" id="noticeUserTitle" class="liger-textbox" name="noticeTitle" style="width: 170px;" />
	                        </td>
	                    </tr>
	                    <tr>
	                        <td valign="top">通知内容：</td>
	                        <td>
	                            <textarea id="noticeUserContent" name="noticeContent" ></textarea>
	                        </td>
	                    </tr>
	                </table>
	                <div>
	                    <input type="button" value="添加" id="addNoticeUser" onclick="f_addNoticeUser()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
	                    <input type="button" value="删除" id="deleteNoticeUser" onclick="f_deleteNoticeUser()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
	                </div>.
	                <div id="mainGridNoticeUser"style="margin-top:10px;">
	
	                </div>
	            </div>
	            <%--通知发起人--%>
	            <div class="notice" style="margin-top:10px;display:none;text-align: center;">
	                <table style="border-collapse:separate; border-spacing:10px;">
	                    <tr>
	                        <td>通知类型：</td>
	                        <td>
	                            <input type="text" id="noticeWay" class="liger-textbox" name="noticeWay" style="width: 170px;background:#ffffff;" value="Email" disabled="disabled" />
	                        </td>
	                    </tr>
	                    <tr>
	                        <td>通知标题：</td>
	                        <td>
	                            <input type="text" id="noticeTitle" class="liger-textbox" name="noticeTitle" style="width: 170px;" />
	                        </td>
	                    </tr>
	                    <tr>
	                        <td valign="top">通知内容：</td>
	                        <td>
	                            <textarea id="noticeContent" name="noticeContent" ></textarea>
	                        </td>
	                    </tr>
	                </table>
	            </div>
	        </div>
	    </div>
	</div>
</body>
	<script src="<%=context %>/gooflow/jquery.min.js"></script>
	<script src="<%=context %>/Source/lib/ligerUI/js/ligerui.all.js"></script>
  	<script type="text/javascript">
	var mainGridRole;
	var mainGridUser;
	var mainGridNoticeHost;
	var mainGridOrganization;
	// 输出条件
	var mainGridConditions;
	var mainGridNoticeRole;
	var mainGridNoticeUser;
  
  	var path = '<%=context %>';
	var processId = '${processId}';
	var childNodeId = '${childNodeId}';
    $(function(){
        //设置通知框的大小
        $("textarea").css({"min-width":"550px","min-height":"100px","max-width":"550px","max-height":"100px"});
        $("input[type=button]").css({"background":"#ffffff","display":"inline-block","cursor":"pointer"});//按钮的样式
        /*绑定方法下拉框*/
        $("#functionName").ligerComboBox({
            url: path+'/flow/function/getComboBoxData?processId='+processId,
            valueField: 'id',
            textField: 'functionName',
            valueFieldID: 'functionId',
            selectBoxWidth: 170,
            width: 170,
            highLight:true,
            cancelable:true
        });

        /*绑定编辑页面下拉框*/
        $("#editPageName").ligerComboBox({
            url:path + '/flow/workPage/getComboBoxData?processId='+processId+'&type=2',
            valueField: 'id',
            textField: 'pageName',
            valueFieldID: 'editPageId',
            selectBoxWidth: 170,
            width: 170,
            highLight:true,
            cancelable:true
        });

        /*输出条件*/
        mainGridConditions = $("#mainGridConditions").ligerGrid({
            columns: [
                { display: 'id', name: 'id', hide: true, width: 60 },
                { display: '序号', width: 40, render: function (rowData, rowindex, value, column, rowid, page, pagesize) { return (page - 1) * pagesize + rowindex + 1; } },
                { display: '条件表达式', name: 'conditions', align: 'center',width:200},
                { display: '下部步节点', name: 'nextNode', align: 'center',width:200}
            ],
            width: '500', heightDiff: -1,height:'30%', usePager:false, checkbox:true
        });

        /*接收通知角色列表*/
        mainGridNoticeRole = $("#mainGridNoticeRole").ligerGrid({
            columns: [
                { display: 'id', name: 'id', hide: true, width: 60 },
                { display: '角色名称', name: 'roleName', align: 'center', width: 200 },
                { display: '角色代码', name: 'id', align: 'center', width: 200 },
            ],
            width: '100%',
        });
        /*执行角色列表*/
        mainGridRole = $("#mainGridRole").ligerGrid({
            columns: [
                { display: 'id', name: 'id', hide: true, width: 60 },
                { display: '角色名称1', name: 'roleName', align: 'center', width: 200 },
                { display: '角色代码', name: 'roleCode', align: 'center', width: 200 },
            ],
            width: '100%',
        });

        /*组织机构*/
        mainGridOrganization = $("#mainGridOrganization").ligerGrid({
            columns: [
                { display: 'id', name: 'id', hide: true, width: 60 },
                { display: '序号', width: 40, render: function (rowData, rowindex, value, column, rowid, page, pagesize) { return (page - 1) * pagesize + rowindex + 1; } },
                { display: '机构名称', name: 'organizationName', align: 'center', width: 200 },
                { display: '机构代码', name: 'organizationCode', align: 'center', width: 200 },
            ],
            width: '100%',
        });

        /*自定义执行人*/
        mainGridUser = $("#mainGridUser").ligerGrid({
            columns: [
                { display: 'id', name: 'id', hide: true, width: 60 },
                //{ display: '序号', width: 40, render: function (rowData, rowindex, value, column, rowid, page, pagesize) { return (page - 1) * pagesize + rowindex + 1; } },
                { display: '帐号', name: 'account', align: 'center',width:100},
                { display: '名称', name: 'userName', align: 'center',width:100},
                { display: '类型', name: 'userType', align: 'center',width:60, render: function (row) {return 'employee' == row['userType'] ? '内部员工' : '合作伙伴'}},
            ],
            width: '100%',
        });
        /*接收通知人列表*/
        mainGridNoticeUser = $("#mainGridNoticeUser").ligerGrid({
            columns: [
                { display: 'id', name: 'id', hide: true, width: 60 },
                { display: '序号', width: 40, render: function (rowData, rowindex, value, column, rowid, page, pagesize) { return (page - 1) * pagesize + rowindex + 1; } },
                { display: '帐号', name: 'account', align: 'center',width:100},
                { display: '名称', name: 'userName', align: 'center',width:100},
                { display: '类型', name: 'userType', align: 'center',width:60, render: function (row) {return 'employee' == row['userType'] ? '内部员工' : '合作伙伴'}},
            ],
            width: '100%', heightDiff: -1,height:'50%',
            usePager:false
        });

        /*
         *接收并初始化节点
         */
        for(var node in nodeObj){
            var nodeChild = nodeObj[node];
            if(nodeChild.type != "start round" && node != sessionStorage.getItem("currentNodeId")){
                $("#node").append("<option value='"+node+"'>"+nodeChild.name+"</option>");
            }
        }
        $("#navtab1").ligerTab();

        /*任务执行人单选按钮的切换*/
        $("input[name=fkType]").click(function(){
            $(".post:eq("+$(this).index()+")").show().siblings().hide();
        });
        /*通知接收人单选按钮的切换*/
        $("input[name=notice]").click(function(){
            $(".notice:eq("+$(this).index()+")").show().siblings().hide();
        });
        loadChildTasks();
        loadStartCondition();
        loadNotice();
    });

    var nodeStr = sessionStorage.getItem("nodeStr");
    var nodeObj = JSON.parse(nodeStr);

    /*加载子任务信息*/
    function loadChildTasks(){
        var childTaskObj = JSON.parse(sessionStorage.getItem("allChildTasks"));
        if(!childTaskObj[childNodeId]){
            return;
        }

        //工作任务名
        var workName = childTaskObj[childNodeId].name;
        $("#workTask").val(workName);
        //下个节点
        var nextNodeId = childTaskObj[childNodeId].nextNodeId;
        if(nodeObj[nextNodeId]){
            var nextNodeName = nodeObj[nextNodeId].name;
            $("#node").val(nextNodeId);
            var count=$("#node option").length;
            for(var i=0;i<count;i++)
            {
                if($("#node").get(0).options[i].text == nextNodeName) {
                    $("#node").get(0).options[i].selected = true;
                    break;
                }
            }
        }

        //编辑页面
        var editPageId = childTaskObj[childNodeId].editPageId;
        if(editPageId){
            $.ajax({
                url:path+'/flow/workPage/getPageNameById',
                type:'post',
                dataType:'json',
                data:{editPageId:editPageId},
                success:function(page){
                    $("#editPageName").ligerComboBox().setText(page.editPageName);
                    $("#editPageId").val(editPageId);
                }
            });
        }
        //执行方法
        var functionId = childTaskObj[childNodeId].functionId;
        if(functionId){
            $.ajax({
                url:path+'/flow/function/getFunctionById',
                type:'post',
                dataType:'json',
                data:{functionId:functionId},
                success:function(func){
                	debugger;
                    $("#functionName").ligerComboBox().setText(func.functionName);
                    $("#functionId").val(functionId);
                }
            });
        }

        // 任务执行人
        var fkId = childTaskObj[childNodeId].fkId;
        var fkIds = fkId.split(",");
        var radioId = childTaskObj[childNodeId].radioId;
        $("#"+radioId).click();
        if(radioId == "post"){//岗位
            $("#post").attr("checked",true);
            for(var i=0;i<fkIds.length;i++){
                $.ajax({
                    url:path+"/bap/position/getPositionByCode",
                    type:"post",
                    dataType:"json",
                    data:{code:fkIds[i]},
                    success:function(model){
                        var manager = $("#mainGrid").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        manager.addRow({
                            id:model.code,
                            hostName:model.name,
                            hostCode:model.code,
                            organization:model.organization.name
                        }, firstRow, true);
                    }
                });
            }
        }else if(radioId == "role"){//角色
            $("#role").attr("checked",true);
            for(var i=0;i<fkIds.length;i++){
                $.ajax({
                    url:path+"/bap/featureRole/getFeturePoleByCode",
                    type:"post",
                    dataType:"json",
                    data:{code:fkIds[i]},
                    success:function(model){
                        var manager = $("#mainGridRole").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        manager.addRow({
                            id:model.role_id,
                            roleName:model.role_name,
                            roleCode:model.role_id
                        }, firstRow, true);
                    }
                });
            }
        } else if(radioId == "organizational"){//组织机构
            $("#organizational").attr("checked",true);
            for(var i=0;i<fkIds.length;i++){
                $.ajax({
                    url:path+"/bap/organization/getOrganizationByCode",
                    type:"post",
                    dataType:"json",
                    data:{code:fkIds[i]},
                    success:function(model){
                        var manager = $("#mainGridOrganization").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        manager.addRow({
                            id:model.code,
                            organizationName:model.name,
                            organizationCode:model.code
                        }, firstRow, true);
                    }
                });
            }
        } else if(radioId == "executive"){//自定义接收人
            $("#executive").attr("checked",true);
            for(var i=0;i<fkIds.length;i++){
                $.ajax({
                    url:path+"/bap/user/getUserAccount",
                    type:"post",
                    dataType:"json",
                    data:{account:fkIds[i]},
                    success:function(model){
                        var manager = $("#mainGridUser").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        manager.addRow({
                            id:model.userId,
                            account:model.account,
                            userName:model.userName,
                            userType:model.userType
                        }, firstRow, true);
                    }
                });
            }
        }else if(radioId == "promoter"){//流程发起人
            $("#promoter").attr("checked",true);
        }
        //条件
        var conditions = childTaskObj[childNodeId].conditions;
        for(var key in conditions){
            var nextNode = nodeObj[conditions[key]].name;
            var manager = $("#mainGridConditions").ligerGetGridManager();
            var firstRow = manager.getRow(0);
            if(key != "default"){
                manager.addRow({
                    id:conditions[key],
                    conditions:key,
                    nextNode:nextNode
                }, firstRow, true);
            }
        }
    }
     // 加载启动条件
    function loadStartCondition(){
    	var childTaskObj = JSON.parse(sessionStorage.getItem("allChildTasks"));
        if(!childTaskObj[childNodeId]){
            return;
        }
        $("#startCondition").html(childTaskObj[childNodeId].preCondition)
    }

    /*加载通知信息*/
    function loadNotice(){
        var childTaskObj = JSON.parse(sessionStorage.getItem("allChildTasks"));
        if(!childTaskObj[childNodeId]){
            return;
        }
        var notices = childTaskObj[childNodeId].notices;
        if(notices){
            var radioId = notices[0].radioId;
            var title = notices[0].title;
            var content = notices[0].content;
            var fkId = notices[0].fkId;
            var fkIds = fkId.split(",");
            $("#"+radioId).click();
            if(radioId =="noticeHost"){
                $("#noticeHostTitle").val(title);
                $("#noticeHostContent").val(content);
                for(var i=0;i<fkIds.length;i++){
                    $.ajax({
                        url:path+"/bap/position/getPositionByCode",
                        type:"post",
                        dataType:"json",
                        data:{code:fkIds[i]},
                        success:function(model){
                            var manager = $("#mainGridNoticeHost").ligerGetGridManager();
                            var firstRow = manager.getRow(0);
                            manager.addRow({
                                id:model.code,
                                hostName:model.name,
                                hostCode:model.code,
                                organization:model.organization.name
                            }, firstRow, true);
                        }
                    });
                }
            }else if(radioId =="noticeRole"){
                $("#noticeRoleTitle").val(title);
                $("#noticeRoleContent").val(content);
                for(var i=0;i<fkIds.length;i++){
                    $.ajax({
                        url:path+"/bap/featureRole/getFeturePoleByCode",
                        type:"post",
                        dataType:"json",
                        data:{code:fkIds[i]},
                        success:function(model){
                            var manager = $("#mainGridNoticeRole").ligerGetGridManager();
                            var firstRow = manager.getRow(0);
                            manager.addRow({
                                id:model.code,
                                roleName:model.name,
                                roleCode:model.code
                            }, firstRow, true);
                        }
                    });
                }
            }else if(radioId =="noticeExecutive"){
                $("#noticeUserTitle").val(title);
                $("#noticeUserContent").val(content);
                for(var i=0;i<fkIds.length;i++){
                    $.ajax({
                        url:path+"/bap/user/getUserAccount",
                        type:"post",
                        dataType:"json",
                        data:{account:fkIds[i]},
                        success:function(model){
                            var manager = $("#mainGridNoticeUser").ligerGetGridManager();
                            var firstRow = manager.getRow(0);
                            manager.addRow({
                                id:model.account,
                                account:model.account,
                                userName:model.userName,
                                userType:model.userType
                            }, firstRow, true);
                        }
                    });
                }
            }else if(radioId =="noticePromoter"){
                $("#noticeTitle").val(title);
                $("#noticeContent").val(content);
            }
        }
    }
    /*创建通知 json对像*/
    var isNoticesNull
    function noticesJson(){
        isNoticesNull = true;
        var str = "";
        var fkType = "";
        var radioId = "";
        var radios = $("input[name=notice]");
        for(var i=0;i<radios.length;i++){
            if(radios[i].checked){
                fkType = radios[i].value;
                radioId = radios[i].id;
            }
        }
        var noticeJson =[];
        if($("#noticeRole").get(0).checked){//角色
            var manager = $("#mainGridNoticeRole").ligerGetGridManager();
            var rows = manager.getData();
            var roles = [];
            $(rows).each(function () {
                roles.push(this.roleCode);
            });
            str = roles.join(",");
            if((str == "" && $("#noticeRoleTitle").val()=="" && $("#noticeRoleContent").val() == "") || (str != "" && $("#noticeRoleTitle").val()!="" && $("#noticeRoleContent").val() != "")){
                noticeJson = [{radioId:radioId,fkId:str,fkType:fkType,noticeWay:$("#noticeRoleWay").val(),
                    title:$("#noticeRoleTitle").val(),content:$("#noticeRoleContent").val()}];
            }else if($("#noticeRoleTitle").val()==""){
                isNoticesNull = false;
                $.ligerDialog.warn("通知标题不能为空")
            }else if($("#noticeRoleContent").val()==""){
                isNoticesNull = false;
                $.ligerDialog.warn("通知内容不能为空")
            }else if(str==""){
                isNoticesNull = false;
                $.ligerDialog.warn("请选择通知接收人")
            }
        }else if($("#noticeExecutive").get(0).checked){//自定义执行人
            var manager = $("#mainGridNoticeUser").ligerGetGridManager();
            var rows = manager.getData();
            var roles = [];
            $(rows).each(function () {
                roles.push(this.account);
            });
            str = roles.join(",");
            if((str == "" && $("#noticeUserTitle").val()=="" && $("#noticeUserContent").val() == "") || (str != "" && $("#noticeUserTitle").val()!="" && $("#noticeUserContent").val() != "")){
                noticeJson = [{radioId:radioId,fkId:str,fkType:fkType,noticeWay:$("#noticeUserWay").val(),
                    title:$("#noticeUserTitle").val(),content:$("#noticeUserContent").val()}];
            }else if($("#noticeUserTitle").val()==""){
                isNoticesNull = false;
                $.ligerDialog.warn("通知标题不能为空")
            }else if($("#noticeUserContent").val()==""){
                isNoticesNull = false;
                $.ligerDialog.warn("通知内容不能为空")
            }else if(str==""){
                isNoticesNull = false;
                $.ligerDialog.warn("请选择通知接收人")
            }
        }else if($("#noticePromoter").get(0).checked){//流程发起人
            if(($("#noticeTitle").val()=="" && $("#noticeContent").val() == "") || ($("#noticeTitle").val()!="" && $("#noticeContent").val() != "")){
                noticeJson = [{radioId:radioId,fkId:'${currentUser}',fkType:fkType,noticeWay:$("#noticeWay").val(),
                    title:$("#noticeTitle").val(),content:$("#noticeContent").val()}];
            }else if($("#noticeTitle").val()==""){
                isNoticesNull = false;
                $.ligerDialog.warn("通知标题不能为空")
            }else if($("#noticeContent").val()==""){
                isNoticesNull = false;
                $.ligerDialog.warn("通知内容不能为空")
            }
        }
        /*判断： 把没有值的属性删除*/
        for(var k in noticeJson[0]){
            if(noticeJson[0][k]==undefined || noticeJson[0][k] == "" || noticeJson[0][k] == null){
                noticeJson = "";
                break;
            }
        }
        return noticeJson;
    }

    /*创建条件表达式 JSON 对象*/
    function createExpressJson(){
        var conditions = {"default":$("#node").val()};
        var manager = $("#mainGridConditions").ligerGetGridManager();
        var rows = manager.getData();
        $(rows).each(function () {
            conditions[this.conditions] = this.id;
        });
        return conditions;
    }

    /*创建子任务节点的json对象*/
    function createJson(){
        var fkType="";//执行人类型
        var radioId="";
        var radios = $("input[name=fkType]");
        for(var i=0;i<radios.length;i++){
            if(radios[i].checked){
                fkType = radios[i].value;
                radioId = radios[i].id;
            }
        }
        //定义json对象的模型，并赋值
        var ret = {radioId:radioId,fkId:fkId(),name:$("#workTask").val(),fkType:fkType,nextNodeId:$("#node").val(), functionId:$("#functionId").val(), editPageId:$("#editPageId").val(),notices:noticesJson(), conditions:createExpressJson(),preCondition: $("#startCondition").html()};//
        /*判断： 把没有值的属性删除*/
        for(var k in ret){
          if(ret[k]==undefined || ret[k] == "" || ret[k] == null || JSON.stringify(ret[k]) =="{}"){
              delete ret[k];
          }
        }
        return ret;
      }

    /*流程的执行人code*/
    function fkId(){
        var str = "";
        if($("#role").get(0).checked){//角色
            var manager = $("#mainGridRole").ligerGetGridManager();
            var rows = manager.getData();
            var roles = [];
            $(rows).each(function () {
                roles.push(this.id);
            });
            str = roles.join(",");
        }else if($("#organizational").get(0).checked){//组织机构
            var manager = $("#mainGridOrganization").ligerGetGridManager();
            var rows = manager.getData();
            var roles = [];
            $(rows).each(function () {
                roles.push(this.id);
            });
            str = roles.join(",");
        }else if($("#executive").get(0).checked){//自定义执行人
            var manager = $("#mainGridUser").ligerGetGridManager();
            var rows = manager.getData();
            var roles = [];
            $(rows).each(function () {
                roles.push(this.id);
            });
            str = roles.join(",");
        }else if($("#promoter").get(0).checked){//流程发起人
            str = '${currentUser}';
        }
        return str;
    }

    function f_addHost() {
        $.ligerDialog.open({title: "选择岗位", width: 450,height:450, allowClose: false, url: '${_ctx}/views/flow/positionList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var hosts = dialog.frame.f_hostJson();
                    for(var i=0;i<hosts.length;i++){
                        var manager = $("#mainGrid").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        var data = manager.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.id == hosts[i].id){
                                isOk = false;
                            }
                        });
                        if(isOk){
                            manager.addRow({
                                id:hosts[i].code,
                                hostName:hosts[i].name,
                                hostCode:hosts[i].code,
                                organization:hosts[i].organization
                            }, firstRow, true);
                        }
                    }

                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }

    /*删除岗位*/
    function f_deleteHost(){
          var manager = $("#mainGrid").ligerGetGridManager();
          manager.deleteSelectedRow();
      }

    /*添加通知接收岗位*/
    function f_addNoticeHost() {
        $.ligerDialog.open({title: "选择岗位", width: 450,height:450, allowClose: false, url: '${_ctx}/views/flow/positionList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var hosts = dialog.frame.f_hostJson();
                    for(var i=0;i<hosts.length;i++){
                        var manager = $("#mainGridNoticeHost").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        var data = manager.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.hostCode == hosts[i].code){
                                isOk = false;
                            }
                        });
                        if(isOk){
                            manager.addRow({
                                id:hosts[i].code,
                                hostName:hosts[i].name,
                                hostCode:hosts[i].code,
                                organization:hosts[i].organization
                            }, firstRow, true);
                        }
                    }

                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }

    /*删除通知接收岗位*/
    function f_deleteNoticeHost(){
        var manager = $("#mainGridNoticeHost").ligerGetGridManager();
        manager.deleteSelectedRow();
    }

    /*流程执行角色*/
    function f_addRloe() {
    	$.ligerDialog.open({title: "选择角色", width: 450,height:450, allowClose: false, url: path+'/html/flowpages/featureList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var roles = dialog.frame.f_roleJson();
                    for(var i=0;i<roles.length;i++){
                        var data = mainGridRole.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.roleCode == roles[i].code){
                                isOk = false;
                            }
                        });
                        if(isOk){
                            mainGridRole.addRow({
    		            		roleCode:roles[i].id,
    		            		id:roles[i].id,
    		            		roleName:roles[i].name
    		            	});
                        }
                    }

                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    /*删除流程执行角色*/
    function f_deleteRole(){
        var manager = $("#mainGridRole").ligerGetGridManager();
        manager.deleteSelectedRow();
    }

    /**
     *添加通知接收角色
     */
    function f_addNoticeRloe() {
    	$.ligerDialog.open({title: "选择角色", width: 450,height:450, allowClose: false, url: path+'/html/flowpages/featureList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var roles = dialog.frame.f_roleJson();
                    for(var i=0;i<roles.length;i++){
                        var data = mainGridNoticeRole.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.roleCode == roles[i].id){
                                isOk = false;
                            }
                        });
                        if(isOk){
                        	mainGridNoticeRole.addRow({
                                id:roles[i].id,
                                roleName:roles[i].name,
                                roleCode:roles[i].id
                            });
                        }
                    }

                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    /*删除通知接收角色*/
    function f_deleteNoticeRole(){
        var manager = $("#mainGridNoticeRole").ligerGetGridManager();
        manager.deleteSelectedRow();
    }

    /**
     *组织机构
     */
    function f_addOrganization() {
    	$.ligerDialog.open({title: "选择组织机构", width: 450,height:450, allowClose: false, url: path+'/html/flowpages/flow_OrganizationList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var organizations = dialog.frame.f_organizationJson();
                    for(var i=0;i<organizations.length;i++){
                        var data = mainGridOrganization.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.id == organizations[i].id){
                                isOk = false;
                            }
                        });
                        if(isOk){
                        	mainGridOrganization.addRow({
                                id:organizations[i].id,
                                organizationName:organizations[i].name,
                                organizationCode:organizations[i].id
                            });
                        }
                    }
                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    /*删除机构*/
    function f_deleteOrganization(){
        var manager = $("#mainGridOrganization").ligerGetGridManager();
        manager.deleteSelectedRow();
    }

    /*自定义执行人*/
    function f_addUser() {
    	$.ligerDialog.open( {width: 450,height:450, allowClose: false, url: path+'/html/flowpages/flow_userList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var users = dialog.frame.f_userJson();
                    for(var i=0;i<users.length;i++){
                        var manager = $("#mainGridUser").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        var data = mainGridUser.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.account == users[i].account){
                                isOk = false;
                            }
                        });
                        if(isOk){
                        	mainGridUser.addRow({
                                id:users[i].id,
                                account:users[i].account,
                                userName:users[i].userName,
                                userType:users[i].userType
                            });
                        }
                    }
                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    /*删除自定义执行人*/
    function f_deleteUser(){
        var manager = $("#mainGridUser").ligerGetGridManager();
        manager.deleteSelectedRow();
    }

    /*添加通知接收人*/
    function f_addNoticeUser() {
    	$.ligerDialog.open( {width: 450,height:450, allowClose: false, url: path+'/html/flowpages/flow_userList.jsp',
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var users = dialog.frame.f_userJson();
                    for(var i=0;i<users.length;i++){
                        var manager = $("#mainGridNoticeUser").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        var data = manager.getData();
                        var isOk = true;
                        $(data).each(function () {
                            if(this.id == users[i].id){
                                isOk = false;
                            }
                        });
                        if(isOk){
                            manager.addRow({
                                id:users[i].account,
                                account:users[i].account,
                                userName:users[i].userName,
                                userType:users[i].userType
                            }, firstRow, true);
                        }
                    }
                    dialog.close();
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    /*删除通知接收人*/
    function f_deleteNoticeUser(){
        var manager = $("#mainGridNoticeUser").ligerGetGridManager();
        manager.deleteSelectedRow();
    }
    
    /*添加启动条件弹窗*/
    function addStartCondition(){
    	$.ligerDialog.open({title:'启动条件设置',width: 550,height:400, allowClose: false, url: path+'/flow/processParameter/addStartCondition?processId='+processId,
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var express = dialog.frame.getExpress();
                    express = JSON.parse(express)
                    if(express.express == ''){
                    	$.ligerDialog.warn('提示内容');
                    }else{
                        $('#startCondition').html(express.express)
                        dialog.close();
                    }
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    
    /*删除启动条件*/
    $("#deleteStartCondition").click(function(){
    	$('#startCondition').html('')
    })

    /*输出条件弹窗*/
    function conditionsDialog(){
    	$.ligerDialog.open({title:'条件设置',width: 550,height:400, allowClose: false, url: path+'/flow/processParameter/conditions?processId='+processId,
            buttons: [
                { text: '确定', onclick: function (item, dialog) {
                    var express = dialog.frame.getExpress();
                    if(dialog.frame.isEmpty =="next"){
                        BAP.show("请输入下步节点");
                    }else if(dialog.frame.isEmpty =="express"){
                        BAP.show("请输入条件表达式");
                    }else{
                        var manager = $("#mainGridConditions").ligerGetGridManager();
                        var firstRow = manager.getRow(0);
                        manager.addRow({
                            id:express.nodeValue,
                            conditions:express.express,
                            nextNode:express.nextNode
                        }, firstRow, true);
                        dialog.close();
                    }
                }
                },
                { text: '关闭', onclick: function (item, dialog) {
                    dialog.close();
                }
                }
            ]
        });
    }
    /*获取条件中节点*/
    function expressNodeId(){
        var array = [];
        var manager = $("#mainGridConditions").ligerGetGridManager();
        var data = manager.getData();
        $(data).each(function () {
            array.push(this.id);
        });
        return array;
    }
    /*删除条件*/
    function f_deleteExpress(){
        var manager = $("#mainGridConditions").ligerGetGridManager();
        manager.deleteSelectedRow();
    }
  </script>
</html>
